<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      default-expand-all
      check-strictly
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { getRoleDetail, assignPerm } from '@/api/settings'
import { arraytoTree } from '@/utils/index'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    async hSubmit() {
      // const res = await getRoleDetail(this.roleId)
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds: permIds })
      this.$emit('success')
    },
    async loadPermission() {
      const res = await getPermissionList()
      this.list = arraytoTree(res.data)
      const data = await getRoleDetail(this.roleId)
      this.$refs.tree.setCheckedKeys(data.data.permIds)
    }
  }
}
</script>

<style></style>
